package com.ruoyi.flowable.commond;

import static org.flowable.eventregistry.impl.util.CommandContextUtil.getDbSqlSession;

import java.io.Serializable;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;

import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.impl.db.DbSqlSession;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.impl.util.CommandContextUtil;

public class GetTableColumnCommentCmd implements Command<String>, Serializable {

    private String tableName;
    private String columnName;

    public GetTableColumnCommentCmd(String tableName, String columnName) {
        this.tableName = tableName;
        this.columnName = columnName;
    }


    @Override
    public String execute(CommandContext commandContext) {
        DbSqlSession session =  CommandContextUtil.getDbSqlSession(commandContext);
        try {
            DatabaseMetaData metaData = session.getSqlSession().getConnection().getMetaData();
            if ("postgres".equals(session.getDbSqlSessionFactory().getDatabaseType())
                    || "cockroachdb".equals(session.getDbSqlSessionFactory().getDatabaseType())) {
                tableName = tableName.toLowerCase(Locale.ROOT);
            }
            String catalog = getDatabaseCatalog();

            String schema = getDatabaseSchema();

            ResultSet resultSet = metaData.getColumns(catalog, schema,tableName, columnName);
            if (resultSet.next())
            {
                return resultSet.getString("REMARKS");
            }
            else {
                return null;
            }

        } catch (SQLException e) {
            throw new FlowableException("Could not retrieve database metadata: " + e.getMessage());
        }
    }

    protected String getDatabaseCatalog() {
        String catalog = null;
        ProcessEngineConfiguration engineConfiguration= CommandContextUtil.getProcessEngineConfiguration();
        if (engineConfiguration.getDatabaseCatalog() != null && engineConfiguration.getDatabaseCatalog().length() > 0) {
            catalog = engineConfiguration.getDatabaseCatalog();
        }

        return catalog;
    }

    protected String getDatabaseSchema() {
        String schema = null;
        ProcessEngineConfiguration engineConfiguration= CommandContextUtil.getProcessEngineConfiguration();
        if (engineConfiguration.getDatabaseSchema() != null && engineConfiguration.getDatabaseSchema().length() > 0) {
            if ("oracle".equals(getDbSqlSession().getDbSqlSessionFactory().getDatabaseType())) {
                schema = engineConfiguration.getDatabaseSchema().toUpperCase(Locale.ROOT);
            } else {
                schema = engineConfiguration.getDatabaseSchema();
            }
        }

        return schema;
    }

}
